//
//  ZYCRecourceWidomFileDetailView.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/23.
//

#import "ZYCRecourceWidomFileDetailView.h"
#import "UIButton+EnlargeEdge.h"
#import "ZYCPublicFolderModel.h"

@interface ZYCRecourceWidomFileDetailView ()

@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UIButton *shareButton;
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *sizeLabel;
@property (nonatomic, strong) UIButton *openButton;
@property (nonatomic, strong) UIButton *downloadButton;

@end
@implementation ZYCRecourceWidomFileDetailView

- (void)setModel:(ZYCPublicFolderModel *)model{
    _model = model;
    _contentLabel.text = model.name;
    _sizeLabel.text = model.fileSize;
    NSURL *filePath = [NSURL URLWithString:[NSString stringWithFormat:@"%@/zd-system-service%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"sdfz_url"],model.filePath]];
    if([[filePath pathExtension] isEqualToString:@"exe"] || [[filePath pathExtension] isEqualToString:@"rar"] || [[filePath pathExtension] isEqualToString:@"zip"] || [[filePath pathExtension] isEqualToString:@"pptx"]){
        _openButton.hidden = YES;
    }else{
        _openButton.hidden = NO;
    }
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.closeButton];
        [self addSubview:self.shareButton];
        [self addSubview:self.iconView];
        [self addSubview:self.contentLabel];
        [self addSubview:self.sizeLabel];
        [self addSubview:self.openButton];
        [self addSubview:self.downloadButton];
        
        [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15.0f);
            make.top.equalTo(self).offset(20.0f + kStatusBarHeight);
            make.size.mas_equalTo(CGSizeMake(8.0f, 13.0f));
        }];
        
        [self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-15.0f);
            make.centerY.equalTo(self.closeButton);
        }];
        
        [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self).offset(135.0f);
            make.size.mas_equalTo(CGSizeMake(51.0f, 60.0f));
        }];
        
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(50.0f);
            make.right.equalTo(self).offset(-50.0f);
            make.top.equalTo(self.iconView.mas_bottom).offset(20.0f);
        }];
        
        [self.sizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self.contentLabel.mas_bottom).offset(25.0f);
        }];
        
        [self.openButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(70.0f);
            make.right.equalTo(self).offset(-70.0f);
            make.bottom.equalTo(self.downloadButton.mas_top).offset(-20.0f);
            make.height.mas_equalTo(40.0f);
        }];
        [self.downloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(70.0f);
            make.right.equalTo(self).offset(-70.0f);
            make.bottom.equalTo(self).offset(-42.0f);
            make.height.mas_equalTo(40.0f);
        }];
        
    }
    return self;
}


- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [[UIButton alloc] init];
        [_closeButton addTarget:self action:@selector(clickCloseAction:) forControlEvents:UIControlEventTouchUpInside];
        [_closeButton setImage:[UIImage imageNamed:@"back_black"] forState:UIControlStateNormal];
        [_closeButton setEnlargeEdgeWithTop:20 right:20 bottom:20 left:20];
    }
    return _closeButton;
}

- (UIButton *)shareButton {
    if (!_shareButton) {
        _shareButton = [[UIButton alloc] init];
        [_shareButton addTarget:self action:@selector(clickShareAction:) forControlEvents:UIControlEventTouchUpInside];
        [_shareButton setTitle:@"分享" forState:UIControlStateNormal];
        _shareButton.titleLabel.font = kCustomFont(@"PingFangSC-Regular", 18.0f);
        [_shareButton setTitleColor:HEX_RGB(@"#2D2D2D") forState:UIControlStateNormal];
    }
    return _shareButton;
}



- (UIImageView *)iconView {
    if (!_iconView) {
        _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fujian"]];
    }
    return _iconView;
}


- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Regular", 18.0f) titleColor:HEX_RGB(@"#2D2D2D") textAlignment:ZZTextAlignmentCenter];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

- (UILabel *)sizeLabel {
    if (!_sizeLabel) {
        _sizeLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Regular", 16.0f) titleColor:HEX_RGB(@"#999999") textAlignment:ZZTextAlignmentCenter];

    }
    return _sizeLabel;
}

- (UIButton *)openButton {
    if (!_openButton) {
        _openButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _openButton.frame = CGRectMake(0,0, kScreenWidth - 2 * 70, 40);
        [_openButton setTitle:@"立即打开" forState:UIControlStateNormal];
        [_openButton setTitleColor:HEX_RGB(kTitleWhiteColor) forState:UIControlStateNormal];
        _openButton.titleLabel.font = kCustomFont(@"PingFangSC-Regular", 16.0f);
        [_openButton addTarget:self action:@selector(clickOpenAction:) forControlEvents:UIControlEventTouchUpInside];
        _openButton.layer.cornerRadius = 20.0f;
        _openButton.layer.masksToBounds = YES;
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
        gradientLayer.colors = @[(__bridge id)HEX_RGB(@"#F6EEB0").CGColor,
                                 (__bridge id)HEX_RGB(@"#009B4C").CGColor];
        gradientLayer.locations = @[@(0.3),@(1.0f)];
        gradientLayer.frame = _openButton.bounds;
        gradientLayer.cornerRadius = 24;
        [_openButton.layer insertSublayer:gradientLayer atIndex:0];
    }
    return _openButton;
}

- (UIButton *)downloadButton {
    if (!_downloadButton) {
        _downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _downloadButton.frame = CGRectMake(0,0, kScreenWidth - 2 * 70, 40);
        [_downloadButton setTitle:@"下载" forState:UIControlStateNormal];
        [_downloadButton setTitleColor:HEX_RGB(kTitleWhiteColor) forState:UIControlStateNormal];
        _downloadButton.titleLabel.font = kCustomFont(@"PingFangSC-Regular", 16.0f);
        [_downloadButton addTarget:self action:@selector(clickDownloadAction:) forControlEvents:UIControlEventTouchUpInside];
        _downloadButton.layer.cornerRadius = 20.0f;
        _downloadButton.layer.masksToBounds = YES;
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
        gradientLayer.colors = @[(__bridge id)HEX_RGB(@"#F6EEB0").CGColor,
                                 (__bridge id)HEX_RGB(@"#009B4C").CGColor];
        gradientLayer.locations = @[@(0.3),@(1.0f)];
        gradientLayer.frame = _downloadButton.bounds;
        gradientLayer.cornerRadius = 24;
        [_downloadButton.layer insertSublayer:gradientLayer atIndex:0];
    }
    return _downloadButton;
}


- (void)clickCloseAction:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(recourceWidomFileDetailViewDidClose)]) {
        [self.delegate recourceWidomFileDetailViewDidClose];
    }
}

- (void)clickOpenAction:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(recourceWidomFileDetailViewDidClickOpen:)]) {
        [self.delegate recourceWidomFileDetailViewDidClickOpen:self.model];
    }
}

- (void)clickDownloadAction:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(recourceWidomFileDetailViewDidClickDownload:)]) {
        [self.delegate recourceWidomFileDetailViewDidClickDownload:self.model];
    }
}

- (void)clickShareAction:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(recourceWidomFileDetailViewDidShare:)]) {
        [self.delegate recourceWidomFileDetailViewDidShare:self.model];
    }
}
@end
